package com.dwjm.shop.merchant;

import com.dwjm.shop.utils.ConnectionDatabaseForMySQLUseJdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * dwjmshop
 * com.dwjm.shop.merchant
 * 类功能描述
 *
 * @author 邓幸福
 * @date 2025/4/15 上午 09:36
 */
public class MerchantDaoImpl implements MerchantDao {

    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rset = null;
    public MerchantDaoImpl() {
        connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
    }

    //登录功能
    @Override
    public boolean login(String account, String password) {
        try {
            pstmt = connection.prepareStatement("select account from t_merchant where account = ? and password = ?");
            pstmt.setString(1, account);
            pstmt.setString(2, password);
            rset = pstmt.executeQuery();
            if (rset.next()) {
                return true;
            }
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //商品添加
    @Override
    public int addGoods(Goods goods) {
        try {
            pstmt = connection.prepareStatement("insert into t_goods(title, price, descr, img, state, mid) values(?, ?, ?, ?, ?, ?)");
            pstmt.setString(1, goods.getTitle());
            pstmt.setBigDecimal(2, goods.getPrice());
            pstmt.setString(3, goods.getDescr());
            pstmt.setString(4, goods.getImg());
            pstmt.setInt(5, 0);
            pstmt.setInt(6, goods.getMid());
            int i = pstmt.executeUpdate();
            return i == 1 ? 1 : 0; // 三目运算符
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
}
